from numpy import recarray
import pandas
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.patches as patch

font = {'family': 'SimHei', "size": 18}
matplotlib.rc('font', **font)

class Draw(object):
    def __init__(self, data:dict, title:str ='') -> None:
        super().__init__()
        self.data = data
        # self.step = 3
        self.title = title

    def max(self):
        seconds = [i['second'] for i in self.data]
        return max(seconds)

    def draw_box(self, step):
        __temp_to_draw={}
        for i in range(0, int(self.max()), step):
            __temp_to_draw[str(i)] = []
            for j in self.data:
                if j['second'] > i and j['second'] <= i+step:
                    __temp_to_draw[str(i)].append(j['possibility'])

        # fig, ax = plt.subplots(1)
        # rect = patch.Rectangle((0.0,0.0),1.5,0.2, fill=False, linewidth=3)
        # ax.add_patch(rect)

        plt.axhline(0.2, ls='-', c='green', linewidth=2)
        plt.axhline(0.8, ls='-', c='red', linewidth=2)
        

        pdata = pandas.DataFrame(dict([(k, pandas.Series(v)) for k, v in __temp_to_draw.items()]))
        pdata.boxplot()

        plt.ylabel('percent')
        plt.xlabel('time / s')
        plt.title(self.title)

        plt.ylim(0.0,1.0)

        plt.show()

    def draw_hist(self):
        pass

if __name__ == '__main__':
    import json
    with open('example_data.json', 'r', encoding='utf-8') as f:
        data = json.load(f)
    drawer = Draw(data)
    drawer.draw_box()